#include <iostream>
#include <string>

#include <cstdio>

#include <unistd.h>
#include <pthread.h>

const std::string toHex(pthread_t id)
{
    char hex[60];
    sprintf(hex, "%p", id);
    return hex;
}

int g_val = 0;

void show()
{
    std::cout << " g_val: " << g_val << " address: " << &g_val << std::endl;
}

void* threadRun(void* args)
{
    const char* str = (const char*)args;
    while (true)
    {
        std::cout << str << " thread id: " << toHex(pthread_self()) << std::endl;
        std::cout << "thread pid: " << getpid();
        show();
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRun, (void*)"Thread 1");
    
    for (int i = 0; i < 5; ++i)
    {
        std::cout << "main thread pid: " << getpid();
        show();
        ++g_val;
        sleep(1);
    }
    
    pthread_join(tid, nullptr);

    return 0;
}
